package cn.bugstack.middleware.db.router.config;

import cn.bugstack.middleware.db.router.DBRouterConfig;
import cn.bugstack.middleware.db.router.dynamic.DynamicDataSource;
import cn.bugstack.middleware.db.router.util.PropertyUtil;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author wcc
 * @description SpringBoot 中用于自动化加载配置的类，主要包括配置的读取和数据源的创建
 * @create
 */
//配置加载和创建数据源
public class DataSourceAutoConfig implements EnvironmentAware {
    private Map<String, Map<String, Object>> dataSourceMap = new HashMap<>();
    //分库数
    private int dbCount;
    //分表数
    private int tbCount;
    @Bean
    public DBRouterConfig dbRouterConfig() {
        return new DBRouterConfig(dbCount, tbCount);
    }
    @Bean
    public DataSource dataSource() {
        Map<Object, Object> targetDataSource = new HashMap<>();
        for (String dbInf : dataSourceMap.keySet()){
            Map<String, Object> objMap = dataSourceMap.get(dbInf);
            targetDataSource.put(dbInf, new DriverManagerDataSource(objMap.get("url").toString(), objMap.get("username").toString(), objMap.get("password").toString()));
        }
        //设置数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(targetDataSource);
        return dynamicDataSource;
    }
/**
 * setEnvironment，读取自定义配置，因为我们要设置的是一个在 yml 配置多组数据源，
 * 这个方法就是为了解析下面的配置信息。
 * router:
 *      jdbc:
 *        datasource:
 *          dbCount: 2
 *          tbCount: 4
 *          list: db01,db02
 *          db01:
 *            driver-class-name: com.mysql.jdbc.Driver
 *            url: jdbc:mysql://127.0.0.1:3306/bugstack_01?useUnicode=true
 *            username: root
 *            password: 123456
 *          db02:
 *            driver-class-name: com.mysql.jdbc.Driver
 *            url: jdbc:mysql://127.0.0.1:3306/bugstack_02?useUnicode=true
 *            username: root
 *            password: 123456
 *
 * */
    @Override
    public void setEnvironment(Environment environment) {
        String prefix = "router.jdbc.datasource.";
        //从调用工程的配置文件中获取分库分表信息 在yml的路径为：router.jdbc.datasource.dbCount
        dbCount = Integer.valueOf(environment.getProperty(prefix + "dbCount"));
        tbCount = Integer.valueOf(environment.getProperty(prefix + "tbCount"));

        String dataSource = environment.getProperty(prefix + "list");
        //"db01", "db02"
        for (String dbInf : dataSource.split(",")){
            //获取数据库配置信息 得到 map<username, root> 类似
            Map<String, Object> dataSourceProps = PropertyUtil.handle(environment,prefix + dbInf, Map.class);
            //map<库名, "driver,url,username,password">
            dataSourceMap.put(dbInf, dataSourceProps);
        }
    }
}
